package base

import (
	"PackingScanApp/models"

	"github.com/astaxie/beego/orm"
)

//-------------------------------------------------------------------------------

//查询单行,dataset 为 实体 指针
func Run_selectRow(dataset interface{}, sql string, ids ...interface{}) error {
	o := orm.NewOrm()
	o.Using("default")
	err := o.Raw(sql, ids).QueryRow(dataset)
	if err != nil {
		return err
	}
	return nil
}

//查询多行，dataset 为 实体切片 指针
func Run_selectRows(dataset interface{}, sql string, ids ...interface{}) error {
	//dataset.(type)
	o := orm.NewOrm()
	o.Using("default")
	_, err := o.Raw(sql, ids).QueryRows(dataset)
	if err != nil {
		return err
	}
	return nil
}

//更新、删除操作,返回 受影响 行数和错误
func Run_exec(sql string, ids ...interface{}) (int64, error) {
	o := orm.NewOrm()
	o.Using("default")
	res, err := o.Raw(sql, ids).Exec()
	if err == nil {
		num, _ := res.RowsAffected() //受影响条数
		return num, nil
	} else {
		return 0, err
	}
}

//插入操作 返回 插入的ID 和 错误
func Run_exec_insert(sql string, ids ...interface{}) (int64, error) {
	o := orm.NewOrm()
	o.Using("default")
	res, err := o.Raw(sql, ids).Exec()
	if err == nil {
		num, _ := res.LastInsertId()
		return num, nil
	} else {
		return 0, err
	}

}

//插入，更新等的事务操作
func Run_exec_database_transaction(drS []models.DatabaseTransaction) error {
	o := orm.NewOrm()
	o.Using("default")
	err := o.Begin()

	if err != nil {
		return err
	}

	var errback error
	errback = nil

	for _, dr := range drS {
		ids := dr.Ids
		_, err := o.Raw(dr.Sql, ids...).Exec()
		if err != nil {
			errback = err
			//panic(err)
			goto Loop //跳出循环
		}
	}
Loop:
	if errback != nil {
		errback = o.Rollback()
	} else {
		errback = o.Commit()
	}
	return errback
}

//------------------------------------------------------------------------------

//server服务器
//查询单行,dataset 为 实体 指针
func Run_selectRow_S(dataset interface{}, sql string, ids ...interface{}) error {
	o := orm.NewOrm()
	o.Using("server")
	err := o.Raw(sql, ids).QueryRow(dataset)
	if err != nil {
		return err
	}
	return nil
}

//查询多行，dataset 为 实体切片 指针
func Run_selectRows_S(dataset interface{}, sql string, ids ...interface{}) error {
	//dataset.(type)
	o := orm.NewOrm()
	o.Using("server")
	_, err := o.Raw(sql, ids).QueryRows(dataset)
	if err != nil {
		return err
	}
	return nil
}

//更新、删除操作,返回 受影响 行数和错误
func Run_exec_S(sql string, ids ...interface{}) (int64, error) {
	o := orm.NewOrm()
	o.Using("server")
	res, err := o.Raw(sql, ids).Exec()
	if err == nil {
		num, _ := res.RowsAffected() //受影响条数
		return num, nil
	} else {
		return 0, err
	}
}

//插入操作 返回 插入的ID 和 错误
func Run_exec_insert_S(sql string, ids ...interface{}) (int64, error) {
	o := orm.NewOrm()
	o.Using("server")
	res, err := o.Raw(sql, ids).Exec()
	if err == nil {
		num, _ := res.LastInsertId()
		return num, nil
	} else {
		return 0, err
	}

}

//插入，更新等的事务操作
func Run_exec_database_transaction_S(drS []models.DatabaseTransaction) error {
	o := orm.NewOrm()
	o.Using("server")
	err := o.Begin()

	if err != nil {
		return err
	}

	var errback error
	errback = nil

	for _, dr := range drS {
		ids := dr.Ids
		_, err := o.Raw(dr.Sql, ids...).Exec()
		if err != nil {
			errback = err
			//panic(err)
			goto Loop //跳出循环
		}
	}
Loop:
	if errback != nil {
		errback = o.Rollback()
	} else {
		errback = o.Commit()
	}
	return errback
}

//------------------------------------------------------------------------------
